package com.ruoyi.app.service;

import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.user.CaptchaException;
import com.ruoyi.common.core.exception.user.CaptchaExpireException;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.redis.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppMailboxMessageBo;
import com.ruoyi.app.domain.vo.AppMailboxMessageVo;
import com.ruoyi.app.domain.entity.AppMailboxMessage;
import com.ruoyi.app.mapper.AppMailboxMessageMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户信件Service业务层处理
 *
 * @author zcc
 * @date 2024-12-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppMailboxMessageService {

    private final AppMailboxMessageMapper appMailboxMessageMapper;

    /**
     * 查询用户信件
     */
    public AppMailboxMessageVo queryById(Long id){
        return appMailboxMessageMapper.selectVoById(id);
    }

    /**
     * 查询用户信件列表
     */
    public TableDataInfo<AppMailboxMessageVo> queryPageList(AppMailboxMessageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppMailboxMessage> lqw = buildQueryWrapper(bo);
        Page<AppMailboxMessageVo> result = appMailboxMessageMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信件列表
     */
    public List<AppMailboxMessageVo> queryList(AppMailboxMessageBo bo) {
        LambdaQueryWrapper<AppMailboxMessage> lqw = buildQueryWrapper(bo);
        return appMailboxMessageMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppMailboxMessage> buildQueryWrapper(AppMailboxMessageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppMailboxMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAuthorId() != null, AppMailboxMessage::getAuthorId, bo.getAuthorId());
        lqw.like(bo.getId() != null, AppMailboxMessage::getId, bo.getId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), AppMailboxMessage::getName, bo.getName());
        lqw.eq(bo.getAuthorId() != null, AppMailboxMessage::getAuthorId, bo.getAuthorId());
        lqw.eq(bo.getGender() != null, AppMailboxMessage::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), AppMailboxMessage::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressValue()), AppMailboxMessage::getAddressValue, bo.getAddressValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressIds()), AppMailboxMessage::getAddressIds, bo.getAddressIds());
        lqw.eq(StringUtils.isNotBlank(bo.getDomicileAddress()), AppMailboxMessage::getDomicileAddress, bo.getDomicileAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), AppMailboxMessage::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), AppMailboxMessage::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getTheme()), AppMailboxMessage::getTheme, bo.getTheme());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), AppMailboxMessage::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), AppMailboxMessage::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getVideo()), AppMailboxMessage::getVideo, bo.getVideo());
        if(bo.getStatus()!=null){
            if(bo.getStatus().equals("0")){
                lqw.eq(true, AppMailboxMessage::getStatus,"0").or()
                    .eq(true, AppMailboxMessage::getStatus,"1");

            }else {
                lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppMailboxMessage::getStatus, bo.getStatus());

            }
        }

        lqw.eq(bo.getUserId() != null, AppMailboxMessage::getUserId, bo.getUserId());
        lqw.eq(bo.getDeptId() != null, AppMailboxMessage::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), AppMailboxMessage::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptEnclosureImage()), AppMailboxMessage::getDeptEnclosureImage, bo.getDeptEnclosureImage());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptEnclosureVideo()), AppMailboxMessage::getDeptEnclosureVideo, bo.getDeptEnclosureVideo());
        lqw.eq(bo.getReply() != null, AppMailboxMessage::getReply, bo.getReply());
        lqw.like(StringUtils.isNotBlank(bo.getReplyName()), AppMailboxMessage::getReplyName, bo.getReplyName());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,AppMailboxMessage::getCreateTime
            , params.get("beginTime"), params.get("endTime"));
        lqw.orderByDesc(AppMailboxMessage::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户信件
     */
    public void insertByBo(AppMailboxMessageBo bo) {
        validateCaptcha(bo.getCode(), bo.getUuid());
        AppMailboxMessage add = MapstructUtils.convert(bo, AppMailboxMessage.class);
        appMailboxMessageMapper.insert(add);
    }

    public Long insertById(AppMailboxMessageBo bo) {
        validateCaptcha(bo.getCode(), bo.getUuid());
        AppMailboxMessage add = MapstructUtils.convert(bo, AppMailboxMessage.class);
        appMailboxMessageMapper.insert(add);
        return add.getId();
    }

    public void validateCaptcha(String code, String uuid) {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
        String captcha = RedisUtils.getCacheObject(verifyKey);
        RedisUtils.deleteObject(verifyKey);
        if (captcha == null) {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha)) {
            throw new CaptchaException();
        }
    }

    /**
     * 修改用户信件
     */
    public void updateByBo(AppMailboxMessageBo bo) {
        validateCaptcha(bo.getCode(), bo.getUuid());
        AppMailboxMessage update = MapstructUtils.convert(bo, AppMailboxMessage.class);
        appMailboxMessageMapper.updateById(update);
    }

    /**
     * 批量删除用户信件
     */
    public void deleteByIds(Collection<Long> ids) {
        appMailboxMessageMapper.deleteBatchIds(ids);
    }
}
